﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Stock : Form
    {
        public Stock()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
        }

        private void Stock_Load(object sender, EventArgs e)
        {
            RefreshData();
        }


        private void RefreshData()
        {
            ProductDB pdb = new ProductDB();
            dataGridView1.DataSource = pdb.GetProducts();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void AddBtn_Click(object sender, EventArgs e)
        {
            ProductDetailsForm pdt = new ProductDetailsForm();
            if (pdt.ShowDialog() == DialogResult.OK)
            {
                Product p = pdt.Product;
                MessageBox.Show("New product added!");
                ProductDB pdb = new ProductDB();
                pdb.Add(p);
                RefreshData();
            }

                }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete the product?", "delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Product p = (Product)dataGridView1.CurrentRow.DataBoundItem;
                ProductDB pdb = new ProductDB();
                pdb.Delete(p);

                RefreshData();
            }
        }

        private void UpdateBtn_Click(object sender, EventArgs e)
        {
            ProductDetailsForm pdt = new ProductDetailsForm();
            Product p = (Product)dataGridView1.CurrentRow.DataBoundItem;

            p = pdt.Product;
            if (pdt.ShowDialog() == DialogResult.OK)
            {
                ProductDB pdb = new ProductDB();
                pdb.Update(p);

                RefreshData();
            }
        }
    }
}
